<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      // 1.移除数组 [1, 2, 3, 4, 2] 中的2。不要直接修改数组，结果返回新的数组
      let arr1 = [1, 2, 3, 4, 2];
      let res1 = arr1.filter((item) => {
        return item !== 2;
      });
      console.log('res1', res1);
      console.log('arr1', arr1);

      // 2.定义一个函数,能在数组 [1, 2, 3, 4, 2] 的 "2"后面添加元素 "JavaScript"。不要直接修改数组 arr，结果返回新的数组
      let arr2 = [1, 2, 3, 4, 2];
      let newArr2 = arr2.map((item) => item);
      newArr2.push('JavaScript');
      newArr2.forEach((item, index, arr) => {
        if (index == 2) {
          arr.splice(2, 0, 'JavaScript');
        }
      });
      console.log(arr2);
      console.log(newArr2);

      // 3.统计数组 [1, 2, 3, 4, 2] 中2出现的次数
      let arr3 = [1, 2, 3, 4, 2];
      let newArr3 = [];
      arr3.forEach((item) => {
        if (item == 2) {
          newArr3.push(item);
        }
      });
      console.log(' 2出现的次数', newArr3.length);

      // 4.找出数组 [1, 2, 3, "JavaScript", 4, "JavaScript", 2, 4, "JavaScript"] 中重复出现过的元素,并用数组将重复元素装起来输出
      let arr4 = [1, 2, 3, 'JavaScript', 4, 'JavaScript', 2, 4, 'JavaScript'];
      let newArr4 = [];
      for (let i = 0; i < arr4.length; i++) {
        if (arr4[i] == 2) {
          newArr4.push(arr4[i]);
        }
        if (arr4[i] == 4) {
          newArr4.push(arr4[i]);
        }
        if (arr4[i] == 'JavaScript') {
          newArr4.push(arr4[i]);
        }
      }
      console.log(newArr4);

      // 5.在数组里面输出年龄小于17的对象
      let arr5 = [
        {
          name: '111',
          sex: 'boy',
          age: 18,
        },
        {
          name: '222',
          sex: 'girl',
          age: 17,
        },
        {
          name: '333',
          sex: 'boy',
          age: 16,
        },
        {
          name: '444',
          sex: 'girl',
          age: 15,
        },
        {
          name: '555',
          sex: 'boy',
          age: 20,
        },
      ];
      let res5 = arr5.filter((item) => {
        return item.age < 17;
      });
      console.log('res5', res5);

      // 6. 数组扁平化, 把数组[1, [2, [3, 4, 5]]]转成[1, 2, 3, 4，5]
      let arr6 = [1, [2, [3, 4, 5]]];
      let res6 = arr6.flat(2);
      console.log('res6', res6);

      // 7. 使用reduce实现[1,2,3,4,9]数组中所有成员的和
      let arr7 = [1, 2, 3, 4, 9];
      let res7 = arr7.reduce((prve, item) => {
        return prve + item;
      });
      console.log(res7);
      // 8. 数组去重的方法 (4种)
      var arr8 = [1, 2, 3, 5, 2, 4, 3];
      // 方法一 new Set
      // let res8 = [...new Set(arr8)];
      // console.log('res8', res8);

      // 方法二
      // var newArr = [];
      // for (var i = 0; i < arr8.length; i++) {
      //   if (newArr.indexOf(arr8[i]) === -1) {
      //     newArr.push(arr8[i]);
      //   }
      // }
      // console.log('newArr8', newArr);

      // 方法三
      // let res8 = arr8.sort(function (a, b) {
      //   return a - b;
      // });
      // for (let i = 0; i < res8.length; i++) {
      //   if (res8[i] === res8[i + 1]) {
      //     res8.splice(i, 1);
      //     i--;
      //   }
      // }
      // console.log('res8', res8);

      // 方法四
      let obj = {};
      arr8.forEach((item) => {
        obj[item] = '111';
      });
      let newArr = [];
      for (let key in obj) {
        newArr.push(key - 0);
      }
      console.log('newArr', newArr);
    </script>
  </body>
</html>
